package com.se.controller;

import com.se.feign.UserFeignClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import com.se.po.CommonResult;

import java.util.List;

/**
 * @author fanxd
 */
@RestController
@RequestMapping("/cart")
public class CartController {
	@Autowired
	private UserFeignClient client;

//	@Autowired
//	private RestTemplate restTemplate;

//	@Autowired
//	private DiscoveryClient discoveryClient;
	@GetMapping("/getUserById/{userId}")
	//通过服务提供者名(provider-server)获取Eureka server上的元数据
	public CommonResult getUserById(@PathVariable("userId") Integer userId){
//		if (userId == 3) {
//			throw new RuntimeException();
//		}
		return client.getUserById(userId);
//		List<ServiceInstance> instanceList = discoveryClient.getInstances("provider-server");
//
//		//现在，元数据集合中只有一个服务信息
//		ServiceInstance si = instanceList.get(0);

//		CommonResult result = restTemplate.getForObject(
//				"http://provider-server/user/getUserById/"+userId, CommonResult.class);

		//使用Discoveryclient获取元数据，主机地址与端口就可以不硬编码了
//		CommonResult result = restTemplate.getForObject(
//				"http://" + si.getHost() + ":" + si.getPort() + "/user/getUserById/"+userId, CommonResult.class);

        //使用getForObject方法调用提供者微服务
//		CommonResult result = restTemplate.getForObject(
//                     "http://localhost:11000/user/getUserById/"+userId, CommonResult.class);
//		return result;
	}
}
